﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using VM.Shared.Model;
using VM.Shared.ReturnValue;
using System.Windows;
using System.Web;
using PopupWinTool.Common;

namespace GZHJ.Hardware
{
    internal class Program
    {
        /// <summary>
        /// 主方法
        /// </summary>
        /// <param name="args"></param>
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                //计算通道平衡度测试();
                //反射调用重载方法测试();
                //dll弹窗测试();
                //测试完成打印标签测试();
                //计算平均值测试();

                //奥普新音频分析仪.音频分析仪 音频分析仪 = new 奥普新音频分析仪.音频分析仪();
                //var ret = 音频分析仪.输入通道切换(1,out string message);
                //if (!ret)
                //{
                //    Console.WriteLine("输入通道切换失败，" + message);
                //}

                //读取MAC地址测试
                var ss = "303A43333A41323A46303A33353A4332";
                var byys = HexToByte(ss);
                var ascStr = Encoding.ASCII.GetString(byys);
                Console.WriteLine(ascStr);
                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.ReadLine();
        }

        private static void 测试完成打印标签测试()
        {
            Test.完成 test = new Test.完成();
            var ret = test.成功(new TestInfo()
            {
                DicTestResult = new Dictionary<string, TestResultModel>()
                {
                    { "读取SN",new TestResultModel(){ TestValue = "7F  3622  C  R5140005" } },
                }
            });
        }
        private static void dll弹窗测试()
        {
            //dll弹窗测试
            //Main方法必须加上 [STAThread] ，否则会报错
            PopupWinTool.弹窗工具 弹窗 = new PopupWinTool.弹窗工具();
            var ret = 弹窗.弹出测试窗体2(out string ss);
            if (ret == true)
            {
                Console.WriteLine($"传输值：{ss}");
            }
        }
        private static void 反射调用重载方法测试()
        {
            string path = @"D:\工作代码\GZHJ.Hardware\00ExpansionInterface" + @"\MESLib.dll";
            // 加载DLL
            Assembly assembly = Assembly.LoadFrom(path);
            var ss = assembly.GetTypes();
            // 获取DLL中的类型
            Type type = assembly.GetType("MESLib.MES");
            // 创建这个类型的实例
            object instance = Activator.CreateInstance(type);
            ////string mesUrl = "http://172.16.43.200:8090/jeecg-boot/iot/base/query";
            string mesUrl = "http://127.0.0.1:8090/jeecg-boot/iot/base/query";
            string sn = "123456789098765";
            //List<string> snList = new List<string>() { "123456789012345", "123456789012346" };
            DevInfo devInfo = new DevInfo();
            devInfo.FactoryId = "111111";
            devInfo.DevName = "HGS";
            devInfo.Business = "222222";
            devInfo.DeviceCode = "333333";
            TestInfo testInfo = new TestInfo();
            testInfo.DicTestResult = new Dictionary<string, TestResultModel>();
            testInfo.DicTestResult.Add("读取蓝牙Mac", new TestResultModel() { TestItemName = "读取蓝牙Mac", TestValue = "1234567890" });  //绑定蓝牙mac必须增加此内容，否则会抛异常
            testInfo.DicTestResult.Add("读取SN", new TestResultModel() { TestItemName = "读取SN", TestValue = "FFFFFFFFFFFFFF" });  //绑定蓝牙mac必须增加此内容，否则会抛异常

            //SN校验
            //MethodInfo method = type.GetMethod("SNCheck", new Type[] { typeof(string), typeof(string), typeof(DevInfo) });
            //ReturnError result = (ReturnError)method.Invoke(instance, new object[] { mesUrl, sn, devInfo });
            //Console.WriteLine("SN校验请求：" + result.Request);
            //Console.WriteLine("SN校验响应：" + result.Response);


            //绑定蓝牙测试
            MethodInfo method = type.GetMethod("Bind", new Type[] { typeof(string), typeof(string), typeof(TestInfo), typeof(DevInfo) });
            if (method == null)
            {
                return;
            }
            // 调用方法
            ReturnError result = (ReturnError)method.Invoke(instance, new object[] { mesUrl, sn, testInfo, devInfo });
            ////非反射方法调用
            //MESLib.MES mes = new MESLib.MES();
            //var result = mes.Bind(mesUrl, sn, testInfo, devInfo);
            if (!result.IsOK)
            {
                Console.WriteLine($"调用失败，{result.ErrStr}");
            }
            Console.WriteLine("完成绑定请求：" + result.Request);
            Console.WriteLine("完成绑定响应：" + result.Response);


            //上传采集测试项数据
            //MethodInfo method = type.GetMethod("CollectData", new Type[] { typeof(string), typeof(List<string>), typeof(TestInfo), typeof(DevInfo) });
            //ReturnError result = (ReturnError)method.Invoke(instance, new object[] { mesUrl, new List<string>() { "123456ABCDEF"}, testInfo, devInfo });
            //非反射方法调用，方便调试
            //MESLib.MES mes = new MESLib.MES();
            //var result = mes.CollectData(mesUrl, new List<string>() { "123456ABCDEF" }, testInfo, devInfo);
            //if (!result.IsOK)
            //{
            //    Console.WriteLine($"调用失败，{result.ErrStr}");
            //}
            //Console.WriteLine("完成绑定请求：" + result.Request);
            //Console.WriteLine("完成绑定响应：" + result.Response);

        }
        private static void 计算通道平衡度测试()
        {
            double[] dArray = new double[] { 1.94, 1.95, 1.99, 1.99 };
            测试工具.音频测试 helper = new 测试工具.音频测试();
            //var ret = helper.通道平衡度(dArray, 2, 3);
            var ret = helper.通道平衡度_拓展(dArray, 2, 3, 0.001);
            Console.WriteLine(ret);
        }
        private static void 计算平均值测试()
        {
            // 示例 List<double>
            List<double> list1 = new List<double> { 1.0, 2.0, 3.0, 4.0, };
            List<double> list2 = new List<double> { 4.0, 5.0, 6.0, 7.0, };
            List<double> list3 = new List<double> { 7.0, 8.0, 9.0, 9.5, };
            List<double> list4 = new List<double> { 7.0, 8.0, 9.0, 9.5, };
            List<double> list5 = new List<double> { 7.0, 8.0, 9.0, 9.5, };
            List<double> list6 = new List<double> { 7.0, 8.0, 9.0, 9.5, };

            // 将多个 List<double> 放入一个列表中
            List<List<double>> lists = new List<List<double>> { list1, list2, list3, list4, list5, list6 };
            var resultList = GetAverageValueByList(lists);
            Console.WriteLine($"{string.Join("，", resultList)}");
        }
        /// <summary>
        /// 获取嵌套列表的列表平均值
        /// </summary>
        /// <returns></returns>
        private static List<double> GetAverageValueByList(List<List<double>> list)
        {
            // 确保所有List长度相同
            int listLength = list[0].Count;
            if (!list.All(obj => obj.Count == listLength))
            {
                //LogHelper.Error($"[音频分析仪.GetAverageValueByList]获取嵌套列表的列表平均值失败，列表长度不相同，默认使用第一个数据的值");
                return list[0];  //所有List的长度不同，默认使用List[0]的值
            }

            List<double> averageList = new List<double>();
            for (int i = 0; i < list[0].Count; i++)
            {
                double average = list.Average(obj => obj[i]);
                averageList.Add(average);
            }
            return averageList;
        }
        /// <summary>
        /// 16进制转换成byte
        /// </summary>
        /// <param name="hexStr">16进制值</param>
        /// <returns>null 传递值为空或非正确的16进制值，其他则成功转换</returns>
        private static byte[] HexToByte(string hexStr)
        {
            hexStr += "";
            hexStr = hexStr.Trim().Replace(" ", ""); //去掉16进制中间的空格
            if (0 == hexStr.Length || hexStr.Length % 2 != 0)
            {
                return null;
            }

            byte[] buffer = new byte[hexStr.Length / 2];
            for (int i = 0; i < hexStr.Length; i += 2)
            {
                buffer[i / 2] = Convert.ToByte(hexStr.Substring(i, 2), 16);
            }

            return buffer;
        }
    }
}
